package com.ccys.common.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @program: WhiteCoat
 * @description: App返回基础对象
 * @author: ZhangJingCan
 * @create: 2024-01-13 15:29
 **/

@Data
@SuppressWarnings("unchecked")
public class ResponseResultApp<T> implements Serializable {

    // 生成新的序列化ID以避免与原始类冲突
    private static final long serialVersionUID = 5935352669960485206L;

    // 错误码字段
    @JsonProperty("errorCode")
    private int errorCode;

    // 消息字段
    private String message;

    // 数据字段
    private T data;

    // 默认构造器
    public ResponseResultApp() {
        this.errorCode = 0; // 默认成功代码
        this.message = "Success"; // 默认成功消息
        this.data = null;
    }

    // 构造器，带有数据
    public ResponseResultApp(T data) {
        this.errorCode = 0; // 默认成功代码
        this.message = "Success"; // 默认成功消息
        this.data = data;
    }

    // 成功响应的静态方法，无数据
    public static <T> ResponseResultApp<T> success() {
        return new ResponseResultApp<>();
    }

    // 成功响应的静态方法，带有数据
    public static <T> ResponseResultApp<T> success(T data) {
        return new ResponseResultApp<>(data);
    }

    // 成功响应的静态方法，自定义消息和数据
    public static <T> ResponseResultApp<T> success(String message, T data) {
        ResponseResultApp<T> response = new ResponseResultApp<>();
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    // 错误响应的静态方法，无数据
    public static <T> ResponseResultApp<T> error() {
        ResponseResultApp<T> response = new ResponseResultApp<>();
        response.setErrorCode(400);
        response.setMessage(null);
        return response;
    }

    // 错误响应的静态方法，自定义消息
    public static <T> ResponseResultApp<T> error(String message) {
        ResponseResultApp<T> response = new ResponseResultApp<>();
        response.setErrorCode(400);
        response.setMessage(message);
        return response;
    }

    // 错误响应的静态方法，自定义错误代码和消息
    public static <T> ResponseResultApp<T> error(int errorCode, String message) {
        ResponseResultApp<T> response = new ResponseResultApp<>();
        response.setErrorCode(errorCode);
        response.setMessage(message);
        return response;
    }

    // 错误响应的静态方法，自定义错误代码和消息和响应数据
    public static <T> ResponseResultApp<T> error(int errorCode, String message,T data) {
        ResponseResultApp<T> response = new ResponseResultApp<>();
        response.setErrorCode(errorCode);
        response.setMessage(message);
        response.setData(data);
        return response;
    }
    // 更新操作的静态方法
    public static <T> ResponseResultApp<T> update(Integer i, T successData) {
        if (i < 1) {
            return ResponseResultApp.error("操作失败,请刷新后重试");
        }
        return ResponseResultApp.success(successData);
    }


}
